package com.astro.business;

import java.util.Date;
import java.util.Vector;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.astro.EbppException;
import com.astro.common.Constants;
import com.checkfree.isolutions.customer.persist.Account;
import com.checkfree.isolutions.customer.persist.AccountHomeImpl;
import com.checkfree.isolutions.customer.persist.Merchant;
import com.checkfree.isolutions.customer.persist.MerchantHomeImpl;
import com.checkfree.isolutions.statement.persist.Statement;
import com.checkfree.isolutions.statement.persist.StatementHomeImpl;

@Service("statementManager")
@Transactional(value = "transactionManager", propagation = Propagation.REQUIRED)
public class StatementManager extends BaseManager {

	/**
	 * Get a list of statements by ACCOUNT No and Date Range
	 */
	@SuppressWarnings("unchecked")
	public Vector<Statement> getStatements(String accountNo, Date startDate,
			Date endDate) throws Exception {

		Vector<Statement> vecStatements = null;
		try {

			Merchant theMerchant = MerchantHomeImpl.singleton().findByName(
					Constants.MERCHANT);
			if (theMerchant == null) {
				throw new EbppException(
						"unable to obtain Persist Merchant object");
			}
			Account account = AccountHomeImpl.singleton()
					.findByExternalKeyAndMerchant(accountNo, theMerchant);
			if (account == null) {
				throw new EbppException(
						"unable to obtain Persist Account object");
			} else {
				vecStatements = StatementHomeImpl.singleton().findByAccount(
						account, startDate, endDate);
			}

		} catch (Exception e) {
			throw e;
		} catch (Throwable e) {
			new Exception(e);
		}
		return vecStatements;
	}

	/**
	 * Get a statement by ACCOUNT NO and STATEMENT DATE
	 */
	public Statement getStatement(String accountNo, Date statementDate) throws Exception {

		Statement statement = null;
		try {

			Merchant theMerchant = MerchantHomeImpl.singleton().findByName(
					Constants.MERCHANT);
			if (theMerchant == null) {
				throw new EbppException(
						"unable to obtain Persist Merchant object");
			}
			Account account = AccountHomeImpl.singleton()
					.findByExternalKeyAndMerchant(accountNo, theMerchant);
			if (account == null) {
				throw new EbppException(
						"unable to obtain Persist Account object");
			} else {

				statement = StatementHomeImpl.singleton().findByAccountAndDate(account, statementDate);
			}

		} catch (Exception e) {
			throw e;
		} catch (Throwable e) {
			new Exception(e);
		}
		return statement;
	}
}